<template>
  <el-dialog :title="formData.id?'编辑经纪':'新增经纪'" :visible="dialogVisible" @close="btnCancel">
    <el-form ref="client" label-width="80px" :model="formData" :rules="rules">
      <el-form-item label="名称" prop="name">
        <el-input v-model="formData.name" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="formData.email" />
      </el-form-item>
      <el-form-item label="电话">
        <el-input v-model="formData.phone" />
      </el-form-item>
      <el-form-item label="地址">
        <el-input v-model="formData.address" />
      </el-form-item>
      <el-form-item label="描述">
        <el-input v-model="formData.desc" type="textarea" />
      </el-form-item>
      <el-form-item label="封面">
        <ImageUpload ref="cover" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" size="small" @click="confirmFn">确定</el-button>
          <el-button size="small" @click="btnCancel">取消</el-button>
        </el-col>
      </el-row>
    </template></el-dialog>
</template>

<script>
import { addAgent, editAgent, searchAgent } from '@/api/agent'
export default {
  // 父传子
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        name: '',
        email: '',
        phone: '',
        address: '',
        desc: '',
        image: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        email: [
          { required: true, pattern: /^\w+@[a-z0-9]+\.[a-z]{2,4}$/, message: '邮箱格式不正确', trigger: 'blur' }
        ]
      }
    }
  },
  async created() {
  },
  methods: {
    // 关闭弹框
    btnCancel() {
      this.clear()
      // 通知父组件，更改值
      this.$emit('update:dialogVisible', false)
    },
    // 清空数据
    clear() {
      // 清空数据
      this.formData = {
        name: '',
        email: '',
        phone: '',
        address: '',
        desc: '',
        image: ''
      }
      // 清空校验规则
      this.$refs.client.resetFields()
      // 清空照片
      this.$refs.cover.fileList = []
    },
    // 点击确认发送请求
    async confirmFn() {
      try {
        console.log('表单数据', this.formData)
        // 校验规则
        await this.$refs.client.validate()
        // 照片
        const fileList = this.$refs.cover.fileList
        if (fileList.length === 0) {
          this.$message.warning('请添加封面')
          return
        }
        console.log(fileList)
        if (fileList[0] && fileList[0].status !== 'success') {
          this.$message.error('请等图片上传完毕')
          return
        }
        if (fileList[0]) {
          // 有照片才添加url
          this.formData.image = fileList[0].url
        }
        // 发送请求
        // 编辑
        if (this.formData.id) {
          console.log('this.formData', this.formData)
          await editAgent(this.formData)
        } else {
          await addAgent(this.formData)
        }
        //   提示用户
        this.$message.success('操作成功')
        //   关闭弹窗
        this.$emit('update:dialogVisible', false)
        // 刷新页面
        this.$emit('reloadPage')
        this.$emit('reloadNum')
      } catch (error) {
        console.log(error)
      }
    },
    // 编辑功能
    async getAgentId(id) {
      const res = await searchAgent(id)
      this.formData = res.data
      // 第一次点编辑报错！
      // 如果有照片才显示
      if (this.formData.image) {
        // 更新dom异步，直接给个延时器
        setTimeout(() => {
          this.$refs.cover.fileList = [
            { url: this.formData.image }
          ]
        }, 0)
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
